//
//  ViewController.m
//  08--Homework  通讯录
//
//  Created by 冀永金 on 16/7/25.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "VCOfSecond.h"
#import "VCOfThird.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,ThirdDelegate>
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加姓名" style:UIBarButtonItemStylePlain target:self action:@selector(handleNext:)];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
   self.array = [NSArray arrayWithContentsOfFile:path].mutableCopy;
    [self crateTableView];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)crateTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
   
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.array removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [self.array insertObject:@"ddd" atIndex:indexPath.row];
        [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    [_array exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    cell.textLabel.text = [[_array objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VCOfSecond *second = [[VCOfSecond alloc]init];
    [self.navigationController pushViewController:second animated:YES];
    second.name = [[_array objectAtIndex:indexPath.row] objectForKey:@"name"];
    second.phoneNum = [[_array objectAtIndex:indexPath.row] objectForKey:@"phoneNumber"];
    second.gender = [[_array objectAtIndex:indexPath.row] objectForKey:@"gender"];
    second.age = [[_array objectAtIndex:indexPath.row] objectForKey:@"age"];
    
}

- (void)handleNext:(UIBarButtonItem *)item {
    VCOfThird *third = [[VCOfThird alloc]init];
    [self.navigationController pushViewController:third animated:YES];
    third.delegate = self;
    
}
- (void)saveName:(NSMutableDictionary *)mdic {
    [_array addObject:mdic];
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
